<?php 
class Main extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        
		$this->load->library('form_validation');
		$this->load->model('user_model', '', TRUE);
        $this->load->model('society_model', '', TRUE);
        $this->load->model('teacher_model', '', TRUE);
        $this->load->model('photo_model', '', TRUE);
        
        $this->load->library('col_pro_data');
    }
    
    /**
     * 社团后台管理首页
     */
    function index() {
    	
		$condition['society_id'] = $_SESSION['id'];
		$data['society'] = $this->society_model->search_by_condition($condition, TRUE);
		
    	$condition['teacher_id'] = $data['society']['s_teacher'];
		$data['teacher'] = $this->teacher_model->search_by_condition($condition, TRUE);
		
		if (in_array(1, explode(";", $_SESSION['confirm_FLAG']))) {
			$data['msg'] = end(unserialize($data['society']['signin_log']));
		}
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$this->load->view("backend/society/index", $data);
    }
    
    /**
     * 社团基本信息修改
     */ 
	function update_info() {
		
		//1：未开始审核 2：审核完成了 3：在审核了，但是信息有错误，需修改
		if ($_SESSION['confirm_FLAG'] == SOCIETY_TO_VERIFY 
			OR $_SESSION['confirm_FLAG'] == SOCIETY_FINAL_PASS 
			OR in_array(1, explode(";", $_SESSION['confirm_FLAG']))) {
				
			$data['msg'] = "can_modify";
		}
		
		$condition['society_id'] = $_SESSION['id'];
		$data['society'] = $this->society_model->search_by_condition($condition, TRUE);
		
    	$condition['teacher_id'] = $data['society']['s_teacher'];
		$data['teacher'] = $this->teacher_model->search_by_condition($condition, TRUE);
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view('backend/society/update_info', $data);
    }
    
    function save_new_info() {
    	
    	$this->form_validation->set_rules('s_name', '社团名称', 'required|xss_clean');
		$this->form_validation->set_rules('abbr', '社团简称', 'required|xss_clean|min_length[4]|max_length[8]');
		$this->form_validation->set_rules('member_num', '社团人数', 'required|numeric');
		$this->form_validation->set_rules('about', '社团简介', 'required|xss_clean|max_length[10000]');
		$this->form_validation->set_rules('web_site', '社团主页', 'url|prep_url|xss_clean|max_length[50]');
		$this->form_validation->set_rules('contact', '社团负责人', 'required|xss_clean|max_length[50]');
		$this->form_validation->set_rules('contact_phone', '社团负责人手机', 'required|xss_clean|max_length[11]|numeric');
		$this->form_validation->set_rules('contact_qq', '社团负责人QQ号', 'required|xss_clean|numeric|min_length[4]|max_length[15]');
		$this->form_validation->set_rules('since', '创办年份', 'required');
		
    	if ($this->form_validation->run() == FALSE) {
    		$this->update_info();
			return;
		}
		
		//若confirm_FLAG为0;0;0则表示审核尚未开始可以随意修改信息
    	if ($_SESSION['confirm_FLAG'] == SOCIETY_TO_VERIFY) {
    		//将表单中的信息存入数据库
	    	$society_data = $this->_get_form_array(array("t_name", "t_phone"));
			$this->society_model->update($society_data);
			
			$teacher_data['t_name'] = $this->input->post("t_name");
			$teacher_data['t_phone'] = $this->input->post("t_phone");
			$this->teacher_model->update($teacher_data);
    	}
		
    	/* 处理社团审核日志相关 */
		
    	//若confirm_FLAG不为0;0;0则表示审核开始了，开始审核后，若没有某项审核失败，则审核期间不允许修改信息
    	if ($_SESSION['confirm_FLAG'] != SOCIETY_TO_VERIFY) {
    		
    		//若confirm_FLAG中含有1则表示某项审核失败了，需重新提交注册信息
    		if (in_array(1, explode(";", $_SESSION['confirm_FLAG']))) {
    			//将表单中的信息存入数据库
    			$society_data = $this->_get_form_array(array("t_name", "t_phone"));
				$this->society_model->update($society_data);
				
				$teacher_data['t_name'] = $this->input->post("t_name");
				$teacher_data['t_phone'] = $this->input->post("t_phone");
				$this->teacher_model->update($teacher_data);
    			
    			$society_id = $_SESSION['id'];
    			
    			//修改信息后，需要修改社团状态到待初审（session以及数据库），以便再次审核
    			$this->session->set_userdata(array(SESSION_SOCIETY_CONFIRM_FLAG=>SOCIETY_TO_VERIFY));
    			$_SESSION['confirm_FLAG'] = SOCIETY_TO_VERIFY;
    			
    			$new_confirm_FLAG = SOCIETY_TO_VERIFY;
		    	$this->society_model->update_confirm_FLAG($new_confirm_FLAG, $society_id);
				
		    	$signin_log = $this->society_model->get_signin_log($society_id);
		    	
		    	if ($signin_log['signin_log'] == "") {
		    		$signin_log_array_length = 0;
		    	} else {
		    		$signin_log_array = unserialize($signin_log['signin_log']);
		    		$signin_log_array_length = count($signin_log_array);
		    	}
		    	
		    	$signin_log_array[$signin_log_array_length]['execute_user'] = $this->_get_society_name();
				$signin_log_array[$signin_log_array_length]['execute_role'] = "社团负责人";
				/*$signin_log_array[$signin_log_array_length]['society_name'] 
					= $this->society_model->_get_full_name_by_society_id($society_id);*/
				$signin_log_array[$signin_log_array_length]['status'] = "待初审";
				$signin_log_array[$signin_log_array_length]['time'] = time();
				$signin_log_array[$signin_log_array_length]['remark'] = "社团注册信息重新提交成功";
				
		    	$new_signin_log = serialize($signin_log_array);
		    	$this->society_model->update_signin_log($new_signin_log, $society_id);
    		} else {
    			//若confirm_FLAG中不含有1则表示尚无某项审核失败，不可重新提交或修改注册信息
    			//echo "wrong";
    			//return ;
    			redirect(BACKEND_SOCIETY_PATH);
    		}
    	}
		redirect(BACKEND_SOCIETY_PATH);
    }
 
    
    /**
     * 邮箱修改
     */
    function show_change_email() {
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
    	$this->load->view('backend/society/change_email', $data);
    }
    
   	function change_email() {
   		
 		//修改email的常规校验
 		$this->form_validation->set_rules('old_email', '旧邮箱', 'required|email|min_length[4]|max_length[50]');
 		$this->form_validation->set_rules('email', '新邮箱', 'required|email|min_length[4]|max_length[50]');
 		$this->form_validation->set_rules('re_email', '确认邮箱', 'required|email|min_length[4]|max_length[50]|matches[email]');
 		//开始验证
	 	if ($this->form_validation->run() == FALSE) {
			$this->show_change_email();
			return;	
	 		}
 		
		//逻辑校验
 		$this->form_validation->set_rules('change_error', '', 'callback_change_email_check');
 		//开始逻辑校验
 		if ($this->form_validation->run() == FALSE) {
			$this->show_change_email();
			return;
		}
		
		//保存数据
 		$t_m = array('old_email', 're_email');
    	$data = $this->_get_form_array($t_m);
    	$id = $_SESSION['id'];
    	$data['id'] = $id;
    	
    	$this->user_model->update($data);
    	session_destroy();
    	$this->session->sess_destroy();
    	redirect(SOCIETY_LOGIN_PATH);
 	}
 	
	//email修改  逻辑校验的回调函数  
 	function change_email_check() {
 		$old_email = $this->input->post('old_email');
 		$id = $_SESSION['id'];
 		
 		$email = $this->input->post('email');
 		
 		$temp_1 = $this->user_model->get_user_by_email_and_id($old_email, $id);
 		$temp_2 = $this->user_model->get_user_by_email_single($email);
 		
 		if (!$temp_1) {
 			$this->form_validation->set_message('change_email_check', '原邮箱输入错误');
 			return FALSE;
 		}
 		
 		if ($temp_2) {
 		    $this->form_validation->set_message('change_email_check', '新输入的邮箱已存在');
 			return FALSE;
 		}
 	}

	function ajax_validate_old_email() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$id = $_SESSION['id'];
		
		if ($this->user_model->get_user_by_email_and_id($validateValue, $id)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
		}
	}
	
	function ajax_validate_new_email() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$id = $_SESSION['id'];
		
		if (!$this->user_model->get_user_by_email_single($validateValue)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
		}
	}
 	
 	/**
     * 密码修改
     */
	function show_change_password() {
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
    	$this->load->view('backend/society/change_password', $data);
    }
    
 	function change_password() {
 		//修改password的常规校验
 		$this->form_validation->set_rules('old_password', '旧密码', 'required|xss_clean|min_length[6]|max_length[16]');
 		$this->form_validation->set_rules('password', '新密码', 'required|xss_clean|min_length[6]|max_length[16]');
 		$this->form_validation->set_rules('re_password', '确认密码', 'required|matches[password]|xss_clean');
 		//开始验证
	 	if ($this->form_validation->run() == FALSE) {
			$this->show_change_password();
			return;	
	 		}
 		
		//逻辑校验
 		$this->form_validation->set_rules('change_error', '', 'callback_change_password_check');
 		//开始逻辑校验
 		if ($this->form_validation->run() == FALSE) {
			$this->show_change_password();
			return;
		}
		
		//保存 修改的数据 password
 		$t_m = array('old_password', 're_password');
    	$data = $this->_get_form_array($t_m);
    	$id = $_SESSION['id'];
    	$data['id'] = $id;
    	$this->user_model->update($data);
    	redirect(SOCIETY_LOGIN_PATH);
 	}

	//password修改  逻辑校验的回调函数  
	function change_password_check() {
		
		$old_password = $this->input->post('old_password');
 		$id = $_SESSION['id'];
 		
 		$password = $this->input->post('password');
 		
 		$temp_1 = $this->user_model->get_user_by_password_and_id($old_password, $id);
 		
 		if (!$temp_1) {
 			$this->form_validation->set_message('change_password_check', '原密码错误');
 			return FALSE;
 		}
 		
 		if ($old_password == $password) {
 		    $this->form_validation->set_message('change_password_check', '新密码与原密码相同');
 			return FALSE;
 		}
 	}
 	
	function ajax_validate_old_password() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$id = $_SESSION['id'];
		
		if ($this->user_model->get_user_by_password_and_id($validateValue, $id)) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
		}
	}
	
	function ajax_validate_new_password() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$id = $_SESSION['id'];
		$temp = $this->user_model->get_by_id($id);
		$old_password = $temp['password'];
		
		if ($validateValue != $old_password) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
			return ;
		}
	}
 	
	function ajax_validate_s_name() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$condition1['society_id'] = $_SESSION['id'];
		$res1 = $this->society_model->search_by_condition($condition1, TRUE, "");
		
		$condition2['s_name'] = $validateValue;
		$res2 = $this->society_model->search_by_condition($condition2, "", TRUE);
		
		//判断1：如果是该社团本身的名称，则可以使用  2：如果该名称本来就不存在，也可以使用
		if ($res1['s_name'] == $validateValue OR $res2 == 0) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
		}
	}
	
	function ajax_validate_s_abbr() {
		$validateValue = $_GET['fieldValue'];
		$validateId = $_GET['fieldId'];
		
		$arrayToJs = array();
		$arrayToJs[0] = $validateId;
		
		$condition1['society_id'] = $_SESSION['id'];
		$res1 = $this->society_model->search_by_condition($condition1, TRUE, "");
		
		$condition2['abbr'] = $validateValue;
		$res2 = $this->society_model->search_by_condition($condition2, "", TRUE);
		
		//判断1：如果是该社团本身的名称，则可以使用  2：如果该名称本来就不存在，也可以使用
		if ($res1['abbr'] == $validateValue OR $res2 == 0) {
			$arrayToJs[1] = true;
			$this->output->set_output(json_encode($arrayToJs));
		} else {
			for($x=0;$x<1000000;$x++){
				if($x == 990000){
					$arrayToJs[1] = false;
					$this->output->set_output(json_encode($arrayToJs));
				}
			}
		}
	}
}
	